﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class ListAttendance
    {
        private int _teacherid;
        private string _postname;
        private string _teachcode;
        private string _teachname;
        private int _late;
        private int _early;
        private int _truant;
        private int _absenteeism;
        private int _sick;
        private int _leave;
        private float _other;
        private string _othercontect;

        public int TeacherID
        {
            get { return _teacherid; }
            set { _teacherid = value; }
        }

        public string PostName
        {
            get { return _postname; }
            set { _postname = value; }
        }

        public string TeachCode
        {
            get { return _teachcode; }
            set { _teachcode = value; }
        }

        public string TeachName
        {
            get { return _teachname; }
            set { _teachname = value; }
        }

        public int Late
        {
            get { return _late; }
            set { _late = value; }
        }

        public int Early
        {
            get { return _early; }
            set { _early = value; }
        }

        public int Truant
        {
            get { return _truant; }
            set { _truant = value; }
        }

        public int Absenteeism
        {
            get { return _absenteeism; }
            set { _absenteeism = value; }
        }

        public int Sick
        {
            get { return _sick; }
            set { _sick = value; }
        }

        public int Leave
        {
            get { return _leave; }
            set { _leave = value; }
        }

        public float Other
        {
            get { return _other; }
            set { _other = value; }
        }

        public string OtherContect
        {
            get { return _othercontect; }
            set { _othercontect = value; }
        }
    }
    public class Attendance : BaseBR
    {
        private static Attendance _attendanceInstance;
        public static Attendance AttendanceInstance
        {
            get { return _attendanceInstance ?? (_attendanceInstance = new Attendance()); }
        }


        public bool Add(t_Attendance model, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                //效验是否存在，是则修改，否则新增
                int count = (from a in db.t_Attendance
                                 where a.FYear == model.FYear &&
                                 a.FMonth == model.FMonth &&
                                 a.TeacherID == model.TeacherID
                                 select a).Count();
                if (count > 0)
                {
                    t_Attendance model2 = (from a in db.t_Attendance
                                           where a.FYear == model.FYear &&
                                           a.FMonth == model.FMonth &&
                                           a.TeacherID == model.TeacherID
                                           select a).FirstOrDefault();
                    model.RID = model2.RID;
                    db.t_Attendance.ApplyCurrentValues(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败！";
                        result = false;
                    }
                }
                else
                {
                    db.t_Attendance.AddObject(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败！";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool Del(int FYear,int FMonth, int TeacherID , ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                var obj = (from t in db.t_Attendance
                           where t.FYear == FYear &&
                           t.FMonth == FMonth &&
                           t.TeacherID == TeacherID
                           select t).FirstOrDefault();
                if (obj != null)
                    db.t_Attendance.DeleteObject(obj);
                try
                {
                    db.SaveChanges();
                }
                catch
                {
                    msg = "操作失败！";
                    result = false;
                }
            }
            return result;
        }

        public t_Attendance GetModel(int FYear,int FMonth, int TeacherID)
        {
            using (DBEntities db = new DBEntities())
            {
                t_Attendance model = (from t in db.t_Attendance
                           where t.FYear == FYear &&
                           t.FMonth == FMonth &&
                           t.TeacherID == TeacherID
                           select t).FirstOrDefault();
                return model;
            }
        }

        public List<t_Attendance> FindALL(string schoolMark, int year, int month)
        {
            using (DBEntities db = new DBEntities())
            {
                var result = from b in db.t_Attendance
                             where b.SchoolMark == schoolMark && b.FYear == year && b.FMonth == month
                             select b;
                List<t_Attendance> list = result.ToList<t_Attendance>();
                return list;
            }
        }

        public IList<ListAttendance> FindALL(int FYear, int FMonth, int FType)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from a in db.t_Attendance
                           from b in db.TEACHERINFO
                           where a.TeacherID == b.RID &&
                           a.FYear == FYear &&
                           a.FMonth == FMonth
                           select new
                           {
                               TeacherID = a.TeacherID,
                               TeachCode = b.TEACHCODE,
                               TeachName = b.TEACHNAME,
                               Late = a.Late,
                               Early = a.Early,
                               Truant = a.Truant,
                               Absenteeism = a.Absenteeism,
                               Sick = a.Sick,
                               Leave = a.Leave,
                               Other = a.Other,
                               OtherContect = a.OtherContect
                           });
                var obj2 = (from t in obj
                            join c in db.t_PostRelation
                            on t.TeacherID equals c.TeacherID into temp1
                            from c in temp1.DefaultIfEmpty()
                            select new
                            {
                                PostID = c.PostID,
                                TeacherID = t.TeacherID,
                                TeachCode = t.TeachCode,
                                TeachName = t.TeachName,
                                Late = t.Late,
                                Early = t.Early,
                                Truant = t.Truant,
                                Absenteeism = t.Absenteeism,
                                Sick = t.Sick,
                                Leave = t.Leave,
                                Other = t.Other,
                                OtherContect = t.OtherContect
                            });
                var obj3 = (from s in obj2
                            join d in db.t_Post
                            on s.PostID equals d.RID into temp2
                            from p in temp2.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? p.RID == null : p.RID == FType)) 
                            select new ListAttendance
                            {
                                TeacherID = (int)s.TeacherID,
                                PostName = p.PostName,
                                TeachCode = s.TeachCode,
                                TeachName = s.TeachName,
                                Late = (int)s.Late,
                                Early = (int)s.Early,
                                Truant = (int)s.Truant,
                                Absenteeism = (int)s.Absenteeism,
                                Sick = (int)s.Sick,
                                Leave = (int)s.Leave,
                                Other = (float)s.Other,
                                OtherContect = s.OtherContect
                            });
                var m = "";
                return obj3.ToList();
            }
        }

        public IList<ListTeacher> ListTeacher(int FType, string TeachCode, string TeachName)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from b in db.TEACHERINFO
                           join c in db.t_TeacherRelation on b.RID equals c.TeacherID into temp1
                           from d in temp1.DefaultIfEmpty()
                           join a in db.t_TeacherType on d.TypeID equals a.RID into temp2
                           from e in temp2.DefaultIfEmpty()
                           where (FType == -1 ? true : (FType == 0 ? e.TypeName == null : e.RID == FType))
                           //update by lxy 修改考勤管理的对教师姓名工号模糊查询的bug bug33
                           //&& (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                           //&& (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                           && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.Contains(TeachCode.Trim()))
                           && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.Contains(TeachName.Trim()))
                           && b.TEACHCODE != "admin"
                           select new ListTeacher
                           {
                               TeacherID = b.RID,
                               TypeName = e.TypeName,
                               TeachCode = b.TEACHCODE,
                               TeachName = b.TEACHNAME,
                               Sexy = (int)b.SEXY != null ? (int)b.SEXY : 0,
                               Birthday = (DateTime)b.BIRTHDAY != null ? (DateTime)b.BIRTHDAY : System.DateTime.MaxValue
                           });
                return obj.ToList();
            }
        }

        public object YearList()
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from a in db.t_Amount
                           select new
                           {
                               FYear = a.FYear
                           }).Distinct();
                return obj.ToList();
            }
        }
    }
}
